package com.scsystem.xysc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scsystem.xysc.aop.LogOperations;
import com.scsystem.xysc.mapper.UserMapper;
import com.scsystem.xysc.pojo.User;
import com.scsystem.xysc.pojo.vo.ReturnResult;
import com.scsystem.xysc.service.SendssY;
import com.scsystem.xysc.service.UserService;
import io.netty.util.internal.StringUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author zhoub
 */
@RestController
@Transactional
@CrossOrigin
@Tag(name = "用户接口")
@RequestMapping("/User")
public class UserController {
    @Resource
     UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringredis;//redis自带的注入就行
    @Resource
    SendssY sendssY;

    @Resource
    UserService userService;

    @GetMapping("login")
    @Operation(summary = "登录，成功返回true。失败或者为null返回false")
    public Boolean userLog(String userName, String password, HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        User user=new User();
        if(!(StringUtils.isBlank(userName)||StringUtils.isBlank(password))){
            User login = userMapper.userLogin(userName,password);
            user.setUserId(login.getUserId());
            if(login==null){
              return false;
            }
        }
        session.setAttribute("userid",user.getUserId());
        session.setAttribute("userType","user");
        return true;
    }


    @GetMapping("session")
    @Operation(summary = "查看当前登录的session" )
    public String userLog2( HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
//        Object userid = session.getAttribute("userid");
//        Object userType = session.getAttribute("userType");
//        return userid;
        System.out.println(session.getAttribute("userid"));
        System.out.println(session.getAttribute("userType"));
        return "hello";
    }


    @PostMapping("insert")
    @Operation(summary = "注册用户")
    @LogOperations(module = "用户模块",operation = "注册了用户")
    public Boolean insertUser( String name,String passwod,String phone,String code){

        ValueOperations<String, String> opsForValue = stringredis.opsForValue();
        if(!StringUtil.isNullOrEmpty(name)&&StringUtil.isNullOrEmpty(passwod)&&StringUtil.isNullOrEmpty(phone)){
            return false;
        }
        User user=new User();
        user.setUserName(name);
        user.setPassword(passwod);
        user.setPhoneNumber(Long.parseLong(phone));
        final Date date=new Date();
        Date date1=new Date();
        user.setLastLoginTime(date1);
        user.setCreateTime(date);
        if(opsForValue.get(phone)==null){
            return false;
        }

        if(opsForValue.get(phone).equals(code)){
            int insert = userMapper.insert(user);
            return insert == 1;
        }
        return false;
    }

    @PostMapping("updateAddress")
    @Operation(summary = "修改地址")
    @LogOperations(module = "用户模块",operation = "修改了地址")
    public ReturnResult<Boolean> updateAddress(String address,HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        Long userid = (Long) session.getAttribute("userid");
        if(userid==null){
            return new ReturnResult<>(400,"请先登录",false);
        }
        User user = userMapper.selectById(userid);
        user.setAddress(address);
        int i = userMapper.updateById(user);
        return i==1?new ReturnResult<>(200,"修改成功",true):new ReturnResult<>(400,"修改失败",false);
    }

    @PostMapping("updatePhone")
    @Operation(summary = "修改手机号")
    @LogOperations(module = "用户模块",operation = "修改了手机号")
    public ReturnResult<Boolean> updatePhone(String phone,String code,HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        Long userid = (Long) session.getAttribute("userid");
        if(userid==null){
            return new ReturnResult<>(400,"请先登录",false);
        }
        ValueOperations<String, String> opsForValue = stringredis.opsForValue();
        if(opsForValue.get(phone)==null){
            return new ReturnResult<>(400,"验证码错误",false);
        }
        if(opsForValue.get(phone).equals(code)){
            User user = userMapper.selectById(userid);
            user.setPhoneNumber(Long.parseLong(phone));
            int i = userMapper.updateById(user);
            return i==1?new ReturnResult<>(200,"修改成功",true):new ReturnResult<>(400,"修改失败",false);
        }
        return new ReturnResult<>(400,"验证码错误",false);
    }
    @PostMapping("updatePassword")
    @Operation(summary = "修改密码")
    @LogOperations(module = "用户模块",operation = "修改了密码")
    public ReturnResult<Boolean> updatePassword(String password,HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        Long userid = (Long) session.getAttribute("userid");
        if(userid==null){
            return new ReturnResult<>(400,"请先登录",false);
        }
        User user = userMapper.selectById(userid);
        user.setPassword(password);
        int i = userMapper.updateById(user);
        return i==1?new ReturnResult<>(200,"修改成功",true):new ReturnResult<>(400,"修改失败",false);
    }

    @PostMapping("updateName")
    @Operation(summary = "修改用户名")
    @LogOperations(module = "用户模块",operation = "修改了用户名")
    public ReturnResult<Boolean> updateName(String name,HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        Long userid = (Long) session.getAttribute("userid");
        if(userid==null){
            return new ReturnResult<>(400,"请先登录",false);
        }
        User user = userMapper.selectById(userid);
        user.setUserName(name);
        int i = userMapper.updateById(user);
        return i==1?new ReturnResult<>(200,"修改成功",true):new ReturnResult<>(400,"修改失败",false);
    }
    @PostMapping("SelectUser")
    @Operation(summary = "查询用户")
    @LogOperations(module = "用户模块",operation = "查询了用户")
    public ReturnResult<User> selectUser(HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        Long userid = (Long) session.getAttribute("userid");
        if(userid==null){
            return new ReturnResult<>(400,"请先登录",null);
        }
        User user = userMapper.selectById(userid);
        return new ReturnResult<>(200,"查询成功",user);
    }

    @PostMapping("send")
    @Operation(summary = "发送验证码")
    @LogOperations(module = "用户模块",operation = "发送了验证码")
    public ReturnResult<Boolean> send(String phone){
        String code = String.valueOf((int)((Math.random()*9+1)*100000));
        sendssY.Send(phone,code);
        ValueOperations<String, String> opsForValue = stringredis.opsForValue();
        opsForValue.set(phone,code);
        return new ReturnResult<>(200,"发送成功");
    }

    @GetMapping("out")
    @Operation(summary = "安全退出")
    public void getout(HttpServletRequest request){
        HttpSession session = request.getSession();
        User userLogin = (User)session.getAttribute("userLogin");
        session.invalidate();
    }
}







